package ug.co.icom.kyazze.healthticket;

import ug.co.icom.kyazze.healthticket.data.ConnRequests;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ConnRequestAdapter extends ArrayAdapter<ConnRequests> {

	private Context context;
	private ConnRequests[] toUse;

	public ConnRequestAdapter(Context context, int resource, int textViewResourceId,
			ConnRequests[] objects) {

		super(context, resource, textViewResourceId, objects);
		this.context = context;
		this.toUse = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


		View row = convertView;
		ConnRequestAcceptWrapper acceptwrapper = null;

		if (row == null)
		{
			row = inflater.inflate(R.layout.connrequestlist_items, parent, false);
			ConnRequests[] items = toUse;

			TextView title = (TextView) row.findViewById(R.id.textView1);

			title.setText(items[position].user.getFullName()+ " has Requested a connection with you");	
			acceptwrapper = new ConnRequestAcceptWrapper (row);
			row.setTag(acceptwrapper);
			Button acceptButton = (Button) row.findViewById(R.id.button1);
			acceptButton.setTag(position);
		}
		else
		{
			acceptwrapper = (ConnRequestAcceptWrapper) row.getTag();
		}

		acceptwrapper.getButton().setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				ConnRequests cr = new ConnRequests();
				int pos = (Integer) v.getTag();
				cr = toUse[pos];
				Bundle bundle = new Bundle();
	    		bundle.putSerializable("connReq", cr);
	    		
	    		Fragment fragment = new AcceptConnectionFragment();
		    	fragment.setArguments(bundle);
		    	FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
		        FragmentTransaction trans = fragmentManager.beginTransaction();
		        trans.replace(R.id.content_frame, fragment);
		        trans.addToBackStack(null);
		        trans.commit();
			}
		});


		return row;
	}

}
